package com.wisemoresystem.seal.action;

import java.util.List;
import java.util.Map;

import org.ne.framework.data.util.ChkTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.base.tip.Tip;
import com.wisemoresystem.seal.model.t_user;
import com.wisemoresystem.seal.service.SealSetService;
import com.wisemoresystem.seal.service.UserService;
import com.wisemoresystem.seal.util.finger.FingerNewTools;
import com.wisemoresystem.seal.web.SessionVal;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 用户登陆
 * 
 * @author Administrator
 *
 */
@Controller
@Scope("prototype")
public class LoginAction extends ProjBaseAction {

	@Autowired
	private UserService userService;

	@RequestMapping("/login_code")
	public ModelAndView login_code(String code) {
		t_user user = userService.selectUserByBoing(code);
		req.getSession().setAttribute(SessionVal.login_user, user);

		logT.logM("登录", t_user.class, user.get_id());
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/ix/frame.htm");
		return mv;
	}

	/**
	 * 用户登陆
	 * 
	 * @param code
	 *            登陆账号
	 * @param pwd
	 *            密码
	 * @param login_type
	 *            登陆类型
	 * @return
	 */
	@RequestMapping("/login")
	public ModelAndView login(String code, String pwd, String login_type,
			String product_info) {
		ModelAndView mv = new ModelAndView();
		if (ChkTools.isNull(code)) {

			mv.setViewName("/jsp/login");
			return mv;
		}

		if ("密码".equals(login_type)) {
			if (ChkTools.isNull(code) || ChkTools.isNull(pwd)) {
				super.setTipMsg("用户名或密码不能为空!!!", Tip.Type.error);
				mv.setViewName("/jsp/login");
				return mv;
			}
			t_user user = userService.selectUserByUnameAndPwd(code, pwd);
			if (user != null) {
				if (user.getLogin_type().equals("密码验证")) {
					req.getSession().setAttribute(SessionVal.login_user, user);

					logT.logM("登录", t_user.class, user.get_id());

					mv.setViewName("redirect:/ix/frame.htm");
				} else {
					super.setTipMsg("登录失败,请选择BoEing账号登录!!!", Tip.Type.error);
					mv.setViewName("/jsp/login");
				}

			} else {
				super.setTipMsg("登录失败,帐号或密码错误，请重试!!!", Tip.Type.error);
				mv.setViewName("/jsp/login");
			}
		} else if ("指纹".equals(login_type)) {

			if (ChkTools.isNull(code)) {
				super.setTipMsg("Boeing不能为空!!!", Tip.Type.error);
				mv.setViewName("/jsp/login");
				return mv;
			}
			t_user user = userService.selectUserByBoing(code);

			if (user != null) {
				try {
					boolean result = false;
					result = FingerNewTools.login(user.getNote(), pwd);

					if (true == result) {
						req.getSession().setAttribute(SessionVal.login_user,
								user);
						mv.setViewName("redirect:/ix/frame.htm");
					} else {
						super.setTipMsg("登录失败,指纹验证失败!", Tip.Type.error);
						mv.setViewName("/jsp/login");
					}
				} catch (Exception ex) {
					super.setTipMsg("指纹认证失败：指纹服务器异常，请重新认证!", Tip.Type.error);
					ex.printStackTrace();
					mv.setViewName("/jsp/login");
				}

			} else {
				super.setTipMsg("boing账号错误，请核对!", Tip.Type.error);
				mv.setViewName("/jsp/login");
			}
		}

		return mv;
	}

	@RequestMapping("/get_code")
	@ResponseBody
	public String get_code(String code) {
		t_user user = userService.selectUserByBoing(code);
		if (user == null || ChkTools.isNull(user.getNote())) {
			return "false";
		}
		return user.getNote();
	}

	/**
	 * 登出
	 * 
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout() {
		t_user user = WebTools.getCurrentUser();
		if (user != null) {
			logT.logM("退出", t_user.class, user.get_id());
		}

		req.getSession().invalidate();
		return "redirect:login.htm";
	}

	/**
	 * 重新登陆
	 * 
	 * @return
	 */
	@RequestMapping("/relogin")
	public String relogin() {
		return "/jsp/frame/relogin";
	}

	// 压力测试用

	@Autowired
	private SealSetService sealService;

	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> list = sealService.selectSealList1(srh_name,
				page);

		ModelAndView mv = new ModelAndView("/jsp/seal/set/list");
		mv.addObject("list", list);
		return mv;
	}

}
